package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.UserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private MinIoTemplate minIoTemplate;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) throws IOException {
        //转换成流文件
        InputStream inputStream = multipartFile.getInputStream();
        //获取文件后缀名  默认获取到的是   image/png
        String contentType = multipartFile.getContentType();
        //判断是否为空
        if (StringUtils.isEmpty(contentType)) {
            log.warn("未获取到后缀名");
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        //去除分割之后的那段字符  {"image","png"}
        String[] split = contentType.split("/");
        //得到后面的后缀名
        String suffixName = split[1];
        //上传文件
        String s = minIoTemplate.uploadFile("",
                System.currentTimeMillis() + "." + suffixName,
                inputStream,
                "image");
        //将url写入到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setType((short) 0);
        // 这里用的是WmUser的id   不要获取ApUserId
        wmMaterial.setUserId(UserThreadLocal.get().getId());
        wmMaterial.setUrl(s);
        int insert = wmMaterialMapper.insert(wmMaterial);
        if (insert < 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        //将创建好的素材信息，返回给前端  前端接受的是一个对象
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public PageResponseResult selectList(WmMaterialDto wmMaterialDto) {
        //先判空
        if (wmMaterialDto == null) {
            return new PageResponseResult(0,0,0);
        }
        //获取请求参数
        Short isCollection = wmMaterialDto.getIsCollection();
        Integer page = wmMaterialDto.getPage();
        Integer size = wmMaterialDto.getSize();
        //构造分页查询
        Page<WmMaterial> page1 = new Page<>(page, size);
        //构造查询条件
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        //查询已收藏  如果已收藏是0就查询全部
        wrapper.eq(isCollection != null && isCollection != 0, WmMaterial::getIsCollection, isCollection);
        //根据构造的查询条件进行查询
        Page<WmMaterial> wmMaterialPage = wmMaterialMapper.selectPage(page1, wrapper);
        List<WmMaterial> records = wmMaterialPage.getRecords();
        Long total = wmMaterialPage.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(page, size, total.intValue());
        pageResponseResult.setData(records);
        return pageResponseResult;
    }
}
